package com.z.ssmdemo.controller;

import com.z.platform.actingModel.ProxyFactory;
import com.z.platform.controller.AbstractController;
import com.z.platform.controller.MyControllerResult;
import com.z.ssmdemo.constant.KeyContant;

import com.z.ssmdemo.service.LoginService;
import com.z.ssmdemo.service.impl.LoginServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author haonan.zhang
 */
public class LoginController extends AbstractController {

    @Override
    protected MyControllerResult service(HttpServletRequest req, HttpServletResponse resp) {
        String nickName = req.getParameter("nickName");
        String pazzword = req.getParameter("pazzword");
        LoginService loginService = ProxyFactory.getProxy(LoginServiceImpl.class);
        String userName = loginService.getUser(nickName, pazzword);
        if (userName != null) {
            //第一次用户请求进来时，如果客户端带有 就从servlet中根据传进来的jsessionid拿到对应用户
            //的session数据，没有就会创建一个（jsessionid）放到相应头（cookie）中，传回到客户端
            HttpSession session = req.getSession();
            //用户身份唯一标识
            session.setAttribute(KeyContant.USER_NAME_IN_SESSION,userName);
        }
        return MyControllerResult.ok(loginService.getUser(nickName, pazzword));
    }
}
